import { h } from "vue"
import { useStore } from "vuex"
import { RouteRecordRaw } from "vue-router"
import { MenuOption, NIcon } from "naive-ui"

function renderIcon(icon: any) {
    return () => h(NIcon, null, { default: () => h(icon) })
}

export function generatorMenuOptions(menuRoutes: Array<RouteRecordRaw>) {
    const menuOptions: Array<MenuOption> = []
    const store = useStore()

    for (let route of menuRoutes) {
        const menu: MenuOption = {
            key: route.name as string,
            label: route.meta ? route.meta.label : "",
            meta: route.meta,
        }

        if (store.state.userinfo.menus) {
            if (!store.state.userinfo.menus.hasOwnProperty(route.name)) {
                continue
            }

            menu.label = store.state.userinfo.menus[route.name as string]
        }

        if (route.meta && route.meta.icon) {
            menu.icon = renderIcon(route.meta.icon)
        }

        if (route.children && route.children.length > 0) {
            menu.children = generatorMenuOptions(route.children)
        }

        menuOptions.push(menu)
    }

    return menuOptions
}

export function browerInfo() {
    const userAgentString =
        (window.navigator && window.navigator.userAgent) || ""
    const match = userAgentString.match(
        /Android|BlackBerry|iPhone|iPad|iPod|IEMobile|Mobile/
    )
    const browsers = [
        ["Edge", /Edge\/([0-9\._]+)/],
        ["Yandexbrowser", /YaBrowser\/([0-9\._]+)/],
        ["Chrome", /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],
        ["Crios", /CriOS\/([0-9\.]+)(:?\s|$)/],
        ["Firefox", /Firefox\/([0-9\.]+)(?:\s|$)/],
        ["Opera", /Opera\/([0-9\.]+)(?:\s|$)/],
        ["Opera", /OPR\/([0-9\.]+)(:?\s|$)$/],
        ["IE", /Trident\/7\.0.*rv\:([0-9\.]+)\).*Gecko$/],
        ["IE", /MSIE\s([0-9\.]+);.*Trident\/[4-7].0/],
        ["IE", /MSIE\s([6-8]\.0)/],
        ["bb10", /BB10;\sTouch.*Version\/([0-9\.]+)/],
        ["Android", /Android\s([0-9\.]+)/],
        ["IOS", /Version\/([0-9\._]+).*Mobile.*Safari.*/],
        ["Safari", /Version\/([0-9\._]+).*Safari/],
        ["UCBrowser", /UCBrowser\/([0-9\._]+)/],
        ["WeiXin", /MicroMessenger\/([0-9\._]+)/],
    ]

    const info = {
        browser: "",
        version: "",
        mobile: false,
    }

    for (let browser of browsers) {
        if ((browser[1] as RegExp).test(userAgentString)) {
            const _match = (browser[1] as RegExp).exec(userAgentString)

            if (_match && _match.length > 1) {
                const versions = _match[1].split(/[._]/).slice(0, 3)

                if (versions.length > 0) {
                    info.version = versions[0]
                }
            }

            info.browser = browser[0] as string
            info.mobile =
                browser[0] == "Android" ||
                browser[0] == "IOS" ||
                browser[0] == "bb10" ||
                browser[0] == "WeiXin" ||
                (match && match[0]) != null ||
                false
        }
    }

    return info
}
